#include "webpage.h"

#include <ctime>

webpage::webpage(FileList *fl_arg)
{
    if(fl_arg)
        fl = fl_arg;

    file *plik = new file;
    plik -> path = "server.cc";

    fl -> addFileToList(plik);
}

webpage::~webpage()
{
	for(unsigned int i=0;i<=fl->size();i++)
		delete(fl->at(i));
}

QString webpage::getPage(QString url)
{
    clock_t buildtime = clock() / (CLOCKS_PER_SEC / 1000);

    time_t rawtime;
    struct tm *timeinfo;
    char buffer[128];

    time(&rawtime);
    timeinfo = localtime(&rawtime);

    strftime(buffer, 128, "%Y-%m-%d %H:%M:%S", timeinfo);

    QString str;
    QTextStream stream(&str, QIODevice::ReadWrite);
    stream  <<  "<html> "
            <<  "<head> "
            <<  "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" /> "
            <<  "<style type=\"text/css\"> "
            <<  "body, th { font-family:tahoma, verdana, arial, helvetica, sans; font-weight:normal; font-size:9pt; } "
            <<  "body { margin:0; background-color:#24BB5E; padding:10px; } "
            <<  "p { margin:0 } s"
            <<  "a { text-decoration:none;  background-color:Transparent; color:#05F; } "
            <<  "a:visited { color:#55F; } "
            <<  "a:hover { background-color:#EEF; } "
            <<  "img { border-style:none } "
            <<  "td { font-size:10pt; background:#FFF; border:1px solid #BBF } "
            <<  "td img { vertical-align:top } "
            <<  "th, th a, th a:visited { color:#555; font-size:13pt; font-weight:bold; padding-bottom:0; } "
            <<  "#foldercomment { font-size:10pt; color:#888; background:#EEE; padding:3px; border:1px solid #DDD; "
            <<  "border-bottom:3px solid #DDD; margin-top:2px; } "
            <<  "#tools { text-align:right; font-size: 8pt; } "
            <<  "#folder, .big { font-size:14pt; font-weight:bold;  } "
            <<  "#folderlabel, #folderstats, #footer { font-size: 8pt; } "
            <<  "#body { "
            <<  "border-bottom: 4px solid #BBF; s"
            <<  "border-top: 4px solid #BBF; "
            <<  "border-left: 1px dotted #BBF; "
            <<  "border-right: 1px dotted #BBF; "
            <<  "background:#99FF99; "
            <<  "padding:15px; "
            <<  "margin:15px; "
            <<  "} "
            <<  ".comment { font-size:7pt; color:#888; background:#EEE; padding:3px; border:1px solid #DDD; margin-top:2px; } "
            <<  ".button { float:right; padding:5px; padding-top:7px; margin:15px; border:2px solid black; "
            <<  "background:white; font-size:8pt; font-weight:bold; } "
            <<  ".button img { vertical-align:text-bottom; } "
            <<  ".flag { font-weight:bold; font-size:8pt; background:white; color:red; text-align:center; border:1px solid red; } "
            <<  "</style> " 
            <<  "<title>HFS /</title> "
            <<  "<link rel=\"shortcut icon\" href=\"favicon.ico\" /> "
            <<  "</head> "
            <<  "<body> "
            <<  "<div id=folderlabel>folder</div> "
            <<  "<div id=folder>/</div> "
            <<  "<div id=body> ";
    /* here should start file list building */
    stream  <<  "<div class=big>No file</div> ";

    stream  <<  "</div> "
            <<  "<div id=footer> "
            <<  "<br />Servertime: " << buffer;

    buildtime = (clock() / (CLOCKS_PER_SEC / 1000)) - buildtime;
    
    if(buildtime>1000)
        stream  <<  "<br />Build-time: " << buildtime/1000 << "s";
    else
        stream  <<  "<br />Build-time: " << buildtime << "ms";         

    stream  <<  "</div> "
            <<  "</body> "
            <<  "</html> ";
    
    stream.flush();
    
    return str;
}
